/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.<br/>
 * 项目名称 : 支付平台
 * 创建日期 : 2016年5月18日
 * 修改历史 : 
 *     1. [2016年5月18日]创建文件 by chihouliang
 */
package com.sinosoft.sinopay.apiservice.wechat.service;

import org.springframework.stereotype.Service;

import com.sinosoft.sinocloud.platform.base.SinoBaseCheckException;
import com.sinosoft.sinocloud.platform.base.SinoBaseService;
import com.sinosoft.sinocloud.platform.common.util.IpUtil;
import com.sinosoft.sinocloud.platform.common.util.XStreamComponent;
import com.sinosoft.sinopay.api.pay.dto.GetwayPayRequestPara;
import com.sinosoft.sinopay.api.pay.dto.PayRequestReturnApi;
import com.sinosoft.sinopay.api.pay.service.GetwayPayApiService;
import com.sinosoft.sinopay.apiservice.wechat.dto.JsApiPayParaDto;
import com.sinosoft.sinopay.apiservice.wechat.dto.JsPayReturnDto;
import com.sinosoft.sinopay.apiservice.wechat.util.HttpsRequestUtil;
import com.sinosoft.sinopay.apiservice.wechat.util.WechatConfig;

//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import com.sinosoft.sinocloud.platform.base.SinoBaseCheckException;
//import com.sinosoft.sinocloud.platform.base.SinoBaseService;
//import com.sinosoft.sinopay.api.gateway.biz.dto.GetwayPayRequestPara;
//import com.sinosoft.sinopay.api.gateway.biz.dto.PayRequestReturnApi;
//import com.sinosoft.sinopay.api.gateway.service.GetwayPayApiService;
//import com.sinosoft.sinopay.apiservice.wechat.dto.JsPayParaDto;
//import com.sinosoft.sinopay.apiservice.wechat.dto.JsPayReturnDto;
//import com.sinosoft.sinopay.apiservice.wechat.dto.WechatConfigure;
//import com.sinosoft.sinopay.apiservice.wechat.util.HttpsRequestUtil;
//import com.thoughtworks.xstream.XStream;
//import com.thoughtworks.xstream.io.xml.DomDriver;
//import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;
//
///**  
// * 【微信接口api处理】
// * @Description: 微信接口api处理
// * @author chihouliang
// * @date 2016年5月18日 上午11:26:03 
// * @version V1.0  
//*/
@Service("getwayPayApiService73")
public class WechatPayApiServiceImpl extends SinoBaseService implements GetwayPayApiService {

    @Override
    public PayRequestReturnApi payRequest(GetwayPayRequestPara getwayPayRequestPara) throws SinoBaseCheckException, Exception {

        PayRequestReturnApi payRequestReturnApi =new PayRequestReturnApi();
        
        //1、封装生成微信支付需要的dto
        String body = "pay";
        String outTradeNo = getwayPayRequestPara.getPayNo();
        String money=getwayPayRequestPara.getPayAmount();
        int totalFee = Integer.parseInt(money);
        String spBillCreateIP = IpUtil.getLocalIP();
        String notifyUrl = getwayPayRequestPara.getNotifyUrl();
        String openid = getwayPayRequestPara.getOpenId();

        JsApiPayParaDto jsApiPayParaDto = new JsApiPayParaDto(body, outTradeNo, totalFee, spBillCreateIP, notifyUrl, openid);
        String xml = jsApiPayParaDto.getXml(); //传输报文
        String payOrderUrl = WechatConfig.ORDER_URL; //微信预支付下单地址
        //2、微信下单
        HttpsRequestUtil httpsRequestUtil = new HttpsRequestUtil();
        //3、下单结果
        String result = httpsRequestUtil.sendPost(payOrderUrl, xml);
        
        
        
        // 4 解析结果，并返回订单参数
        XStreamComponent xStreamComponent = XStreamComponent.newInstance();
        xStreamComponent.processAnnotations(JsPayReturnDto.class);
        JsPayReturnDto JsPayReturnDto =  (JsPayReturnDto)xStreamComponent.fromXML(result);
//        payRequestReturnApi.setPackageId(JsPayReturnDto.getPrepay_id());
        return payRequestReturnApi;
    }
    //	@Autowired
    //	private LogReportThirdService logReportService;
    //	//	@Autowired
    //	//	private WechatConfig wechatConfig;
    //
    //	/* 
    //	 * 微信支付api
    //	 * <p>Title: payRequest</p> 
    //	 * <p>Description: </p> 
    //	 * @param GetwayPay
    //	 * @throws EbizBaseCheckException
    //	 * @see com.sinosoft.sinopay.api.gateway.service.GetwayPayApiService#payRequest(com.sinosoft.sinopay.facade.gateway.dto.pay.GetwayPayDto) 
    //	*/
    //	public PayRequestReturnApi payRequest(GetwayPayRequestPara getwayPayRequestPara) throws SinoBaseCheckException, Exception {
    //		// 1、记录请求日志
    ////		PayThirdReportLog payThirdReportLog = new PayThirdReportLog();
    //		// 封装支付参数，并返回上一级
    ////        PayRequestReturnDto payRequestReturnDto = new PayRequestReturnDto();
    ////        payRequestReturnDto.setAppId(WechatConfigure.getAppid());
    ////        payRequestReturnDto.setReturnCode("01");
    //        
    //		String body = "";
    //		String detail = "";
    //		String attach = "";
    //		String outTradeNo = getwayPayRequestPara.getPayNo(); //支付号码
    //		int totalFee = Integer.parseInt(getwayPayRequestPara.getPayAmount());
    //		String spBillCreateIP = "";
    //		String notifyUrl = getwayPayRequestPara.getNotifyUrl();
    //		String openid = getwayPayRequestPara.getOpenId();
    //
    //		// 微信下单地址
    //		String payOrderUrl = WechatConfigure.PAY_ORDER_API;
    //
    //		// 2、下单参数封装
    //		JsPayParaDto jsPayParaDto = new JsPayParaDto(body, detail, attach, outTradeNo, totalFee, spBillCreateIP, notifyUrl, openid);
    //
    //		// 4、微信下单
    //		String result = "";
    //		//解决XStream对出现双下划线的bug
    //		XStream xStreamForRequestPostData = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
    //		try {
    //			//将要提交给API的数据对象转换成XML格式数据Post给API
    //			String postDataXML = xStreamForRequestPostData.toXML(jsPayParaDto);
    //
    //			// 下单
    //			HttpsRequestUtil httpsRequestUtil = new HttpsRequestUtil();
    //			result = httpsRequestUtil.sendPost(payOrderUrl, postDataXML);
    //			logger.info("微信支付下单返回信息：" + result);
    //			// 下单出现异常，记录请求日志
    //		} catch (Exception e) {
    //			payThirdReportLog.setStatus("02");
    //			logger.error("请求第三方支付异常：" + e.getMessage());
    //			payRequestReturnDto.setReturnCode("111111");
    //		}
    //		// 6解析下单返回信息
    //		JsPayReturnDto JsPayReturnDto = (JsPayReturnDto) xStreamForRequestPostData.fromXML(result);
    //
    //		// 6、下单成功返回订单号
    //        payRequestReturnDto.setOrderNo(JsPayReturnDto.getPrepay_id());
    //		// 记录日志
    ////		logReportService.thirdPayLogReport(payThirdReportLog);
    //		return payRequestReturnDto;
    //	}
    //
}
